package com.www.applyword.ui.home;

import android.content.Intent;
import android.text.TextUtils;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.HomeAdapter2;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.OrderBean;
import com.www.applyword.bean.OrderItemBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.my.MyEnterpriseActivity;
import com.www.applyword.ui.my.PersonalResumeActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/11/20 0020 11:54
 * version: 1.0
 */
public class EmployedActivity extends BaseActivity {
    @BindView(R.id.rl)
    RecyclerView rlJob;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    private HomeAdapter2 mHotJobAdapter;
    private List<OrderBean> hotJobBeans = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.my_enshrine_activity);
        setTitle("申请列表");
        initDate();
    }

    private void initDate() {
        rlJob.setLayoutManager(new LinearLayoutManager(this));
        rlJob.setItemAnimator(new DefaultItemAnimator());
        mHotJobAdapter = new HomeAdapter2(this);
        rlJob.setAdapter(mHotJobAdapter);

        refresh.setOnRefreshListener(refreshLayout -> getOrderList(true));

        refresh.setOnLoadMoreListener(refreshLayout ->getOrderList(false));

        getOrderList(true);

        mHotJobAdapter.setOnItemClickListener((viewHolder, view, i) -> {
            if (TextUtils.isEmpty(mHotJobAdapter.getItem(i).getOrderItem_UserCompany())){
                startActivity(new Intent(EmployedActivity.this, PersonalResumeActivity.class)
                        .putExtra("user_id", mHotJobAdapter.getItem(i).getOrderItem_UserID()));
            }else {
                startActivity(new Intent(EmployedActivity.this, MyEnterpriseActivity.class)
                        .putExtra("user_id", mHotJobAdapter.getItem(i).getOrderItem_UserID()).putExtra("type", 1));
            }
        });
    }

    @Override
    protected void loadData() {

    }


    private void getOrderList(boolean isRefresh) {
        if (isRefresh) page = 1;
        Map<String, Object> params = new HashMap<>();
        params.put("OrderID", getIntent().getStringExtra("OrderID"));
        params.put("Page", page + "");
        params.put("Rows", Rows + "");
        HttpClient.get(EmployedActivity.this, Constants.GETSIGNUPLIST, params,new CallBack<List<OrderItemBean>>() {
            @Override
            public void onSuccess(List<OrderItemBean> orderBeans) {
                if (isRefresh){
                    refresh.finishRefresh(true);
                    if (orderBeans.size() == 0){
                        showEmpty(true,vsEmpty,0);
                    }else {
                        showEmpty(false, vsEmpty,0);
                    }
                    mHotJobAdapter.replaceAll(orderBeans, true);
                }else {
                    if (orderBeans.size() > 0) {
                        refresh.finishLoadMore(true);
                        mHotJobAdapter.addAll(orderBeans);
                    }else {
                        refresh.finishLoadMoreWithNoMoreData();
                    }
                }
                page++;
            }
        });
    }
}
